const assert = require('assert');
const moment = require('moment');

module.exports = class extends think.Controller {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post()
    };
  }
  async addAction() {
    const searchParams = {
      data: {
        ...this.params.data
      },
      model: 'dict'
    };
    const search = await this.model(this.MODEL).getAction(searchParams);
    if (search.length > 0) {
      return this.fail(1, '该条数据字典中已存在', null);
    }
    const params = {
      data: {
        id: think.uuid('v4'),
        create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        update_time: moment().format('YYYY-MM-DD HH:mm:ss'),
        ...this.params.data
      },
      model: 'dict'
    };
    const result = await this.model(this.MODEL).addAction(params);
    return this.success(result, '新增成功');
  }
  async delAction() {
    const {data} = this.params;
    const params = {
      data,
      model: 'dict'
    };
    const result = await this.model(this.MODEL).delAction(params);
    return this.success(result);
  }
  async editAction() {
    const {data} = this.params;
    const params = {
      data,
      model: 'dict',
      searchdata: {
        id: data.id
      }
    };
    const result = await this.model(this.MODEL).editAction(params);
    return this.success(result);
  }
  async listpageAction() {
    const {data} = this.params;
    const params = {
      data,
      model: 'dict'
    };
    const result = await this.model(this.MODEL).listpageAction(params);
    return this.success(result);
  }
  async listallAction() {
    const {data} = this.params;
    const params = {
      data,
      model: 'dict'
    };
    const result = await this.model(this.MODEL).listallAction(params);
    // const compare = function(obj1, obj2) {
    //   var val1 = obj1.type;
    //   var val2 = obj2.type;
    //   if (val1 < val2) {
    //     return -1;
    //   } else if (val1 > val2) {
    //     return 1;
    //   } else {
    //     return 0;
    //   }
    // };
    return this.success(result);
  }
  async getAction() {
    const data = this.get();
    const params = {
      data,
      model: 'dict'
    };
    const result = await this.model(this.MODEL).getAction(params);
    return this.success(result);
  }
  async gettypeAction() {
    const data = this.get();
    const params = {
      data,
      model: 'dict'
    };
    const result = await this.model(this.MODEL).gettypeAction(params);
    const databack = new Set(result);
    return this.success(Array.from(databack), 'ok');
  }
};

/**
 * @api {GET} /dict/dict/gettype /gettype(获取数据类型数组)
 * @apiName gettype
 * @apiGroup dict
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": [
      "role_id",
        "stu_level",
        "stu_groupid",
        "user_level",
        "Stu_Grade",
        "Stu_Major
    ]
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 1
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   空
                  }
 */
/**
 * @api {GET} /dict/dict/listpage /listpage
 * @apiName listpage
 * @apiGroup dict
 * @apiParam {Number} [page] 页数
 * @apiParam {Number} [pageSize] 数据条数
 * @apiParam {String} [id] 根据ID筛选
 * @apiParam {String} [k] 根据键名筛选
 * @apiParam {String} [type] 根据type筛选
 * @apiParam {String} [val] 根据键值筛选
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": [
            {
                "id": "6396ad5d-84a0-4220-9877-0fa5c60135a7",
                "k": "0",
                "memo": "string",
                "type": "user_level",
                "val": "学生",
                "update_time": "2018-08-09 10:41:58",
                "create_time": "2018-08-09 10:41:58"
            },
            {
                "id": "692c6de1-523f-4592-99f8-4069c646c375",
                "k": "0",
                "memo": "string",
                "type": "role_id",
                "val": "用户",
                "update_time": "2018-08-09 09:36:54",
                "create_time": "2018-08-09 09:36:54"
            },
            {
                "id": "9091c475-3f80-41a6-bcf2-2568595aebd4",
                "k": "0",
                "memo": "string",
                "type": "stu_groupid",
                "val": "web前端",
                "update_time": "2018-08-09 09:36:18",
                "create_time": "2018-08-09 09:36:18"
            },
            {
                "id": "b780462d-9b7d-11e8-9fcb-525400180e73",
                "k": "0",
                "memo": "string",
                "type": "stu_level",
                "val": "组员",
                "update_time": "2018-08-09 00:00:00",
                "create_time": "2018-08-09 00:00:00"
            }
        ]
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 1
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   page:1,
                   page:10,
                   k:0
                  }
 */
